/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ManejoExc.PersonalException;
import Negocio.Empresa;
import Negocio.Equipamiento;
import Negocio.Insumo;
import Negocio.Personal;
import java.sql.Date;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Diego
 */
public class FrmEquipamiento extends javax.swing.JDialog {
    private Personal unPersonal;
    private Empresa unaEmprea;
    private Insumo unInsumo;
    private Equipamiento unEquipamiento;
    DefaultListModel modelEquipamiento = new DefaultListModel();
    

    /**
     * Creates new form FrmEquipamiento
     */
    public FrmEquipamiento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public FrmEquipamiento(java.awt.Frame parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.unaEmprea = unaEmpresa;
        this.LstEquipamiento.setModel(modelEquipamiento);
        this.TxtPersonal.setEditable(false);
        this.txtEquipamiento.setEditable(false);
    }
    
    public void setPersonal(Personal unPersonal){
        this.unPersonal = unPersonal;
        this.TxtPersonal.setText(unPersonal.toString());

        this.cargarEquipamiento();
    }
    
    public void setInsumo (Insumo unInsumo){
        this.unInsumo = unInsumo;
        this.txtEquipamiento.setText(this.unInsumo.getTipoDeInsumo().getNombre());
         
    }
    
    public void cargarEquipamiento(){
        this.modelEquipamiento.clear();
        Equipamiento miRegistro;
        Iterator it = this.unPersonal.getEquipamientos().iterator();
        while(it.hasNext()){
            miRegistro = (Equipamiento) it.next();
            this.modelEquipamiento.addElement(miRegistro);
        }
    
        
    }


    

    
    public void limpiarCampos(){        
        this.txtEquipamiento.setText("");
        this.unInsumo = null;        
        this.RbObligatorio.setSelected(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BtnAceptar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        TxtPersonal = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtEquipamiento = new javax.swing.JTextField();
        CmbFecha = new datechooser.beans.DateChooserCombo();
        jLabel3 = new javax.swing.JLabel();
        RbObligatorio = new javax.swing.JRadioButton();
        BtnFindPersonal = new javax.swing.JButton();
        BtnFindEquipamiento = new javax.swing.JButton();
        BtnCancelar = new javax.swing.JButton();
        BtnGuardar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        LstEquipamiento = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        BtnNuevo = new javax.swing.JButton();
        BtnBorrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        jLabel1.setText("Personal");

        jLabel2.setText("Insumo");

        jLabel3.setText("Fecha");

        RbObligatorio.setText("Obligatorio por Art");

        BtnFindPersonal.setText("...");
        BtnFindPersonal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnFindPersonalActionPerformed(evt);
            }
        });

        BtnFindEquipamiento.setText("...");
        BtnFindEquipamiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnFindEquipamientoActionPerformed(evt);
            }
        });

        BtnCancelar.setText("Cancelar");
        BtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelarActionPerformed(evt);
            }
        });

        BtnGuardar.setText("Guardar");
        BtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnGuardarActionPerformed(evt);
            }
        });

        LstEquipamiento.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstEquipamiento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstEquipamientoMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(LstEquipamiento);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Equipamiento");

        BtnNuevo.setText("Nuevo");
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        BtnBorrar.setText("Borrar");
        BtnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBorrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(RbObligatorio)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(TxtPersonal)
                                    .addComponent(txtEquipamiento)
                                    .addComponent(CmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(BtnFindPersonal)
                                    .addComponent(BtnFindEquipamiento)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BtnNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnBorrar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnCancelar))
                            .addComponent(BtnAceptar, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(TxtPersonal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BtnFindPersonal))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtEquipamiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BtnFindEquipamiento))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(CmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addComponent(RbObligatorio)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BtnNuevo)
                            .addComponent(BtnCancelar)
                            .addComponent(BtnGuardar)
                            .addComponent(BtnBorrar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 194, Short.MAX_VALUE)
                        .addComponent(BtnAceptar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addGap(8, 8, 8)
                        .addComponent(jScrollPane2)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnAceptarActionPerformed

    private void BtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnGuardarActionPerformed
        // TODO add your handling code here:
        try{
            Date unaFecha = Empresa.miUtilidadDeFechas.fechaDb(this.CmbFecha.getText());
            if(this.unEquipamiento == null ){                
                this.unaEmprea.otorgarEquipamiento(this.unPersonal, this.unInsumo,unaFecha,this.RbObligatorio.isSelected());
                
            }else{
                this.unPersonal.ActualizarEquipamiento(this.unEquipamiento, this.unInsumo,this.RbObligatorio.isSelected(),Empresa.miUtilidadDeFechas.fechaDb(this.CmbFecha.getText()));
            }
            JOptionPane.showMessageDialog(this, "La operacion se realizo correctamente.");  
            this.limpiarCampos();           
            this.txtEquipamiento.setText("");
            this.unInsumo = null;          
            this.unEquipamiento = null;
            this.BtnBorrar.setEnabled(false);
            this.BtnCancelar.setEnabled(false);
            this.BtnGuardar.setEnabled(false);
            this.BtnNuevo.setEnabled(true);
            this.cargarEquipamiento();
           
            
        }
        catch(PersonalException ex){
            JOptionPane.showMessageDialog(this,ex.getMessage());
        }

        catch (Exception ex){
            JOptionPane.showMessageDialog(this,ex.getMessage());

        }
         
    }//GEN-LAST:event_BtnGuardarActionPerformed

    private void BtnFindEquipamientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFindEquipamientoActionPerformed
        // TODO add your handling code here:
        FrmFindInsumo ventana = new FrmFindInsumo(this,true,this.unaEmprea);
        ventana.setVisible(true);

    }//GEN-LAST:event_BtnFindEquipamientoActionPerformed

    private void BtnFindPersonalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFindPersonalActionPerformed
        // TODO add your handling code here:
        FrmFindPersonal ventana = new FrmFindPersonal(this, true,this.unaEmprea);
        ventana.setVisible(true);
    }//GEN-LAST:event_BtnFindPersonalActionPerformed

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        this.unEquipamiento = null;
        this.txtEquipamiento.setText("");
        this.unInsumo = null;
        this.RbObligatorio.setSelected(false);
        this.BtnNuevo.setEnabled(false);
        this.BtnCancelar.setEnabled(true);
        this.BtnBorrar.setEnabled(false);
        this.BtnGuardar.setEnabled(true);
        
    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void BtnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBorrarActionPerformed
        // TODO add your handling code here:
        int mc,bc,ch=1;
        mc = JOptionPane.QUESTION_MESSAGE;
        bc = JOptionPane.YES_NO_CANCEL_OPTION;
        ch = JOptionPane.showConfirmDialog (this, "Desea retornar el equipamiento?", "Confirmacion", bc, mc);
        if(ch == 0){
            this.unPersonal.quitarEquipamiento(this.unEquipamiento);
        }   
        this.cargarEquipamiento();
        this.unEquipamiento = null;
        this.unInsumo = null;
        this.txtEquipamiento.setText("");
        this.BtnBorrar.setEnabled(false);
        this.BtnCancelar.setEnabled(false);
        this.BtnGuardar.setEnabled(false);
        this.BtnNuevo.setEnabled(true);
    }//GEN-LAST:event_BtnBorrarActionPerformed

    private void LstEquipamientoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstEquipamientoMouseClicked
        // TODO add your handling code here:        
        this.unEquipamiento = (Equipamiento) this.modelEquipamiento.getElementAt(this.LstEquipamiento.getSelectedIndex());
        this.unInsumo = this.unEquipamiento.getUnInsumo();
        this.txtEquipamiento.setText(this.unEquipamiento.getUnInsumo().getTipoDeInsumo().toString());
        this.CmbFecha.setText(Empresa.miUtilidadDeFechas.fechaAr(this.unEquipamiento.getFecha()));
        this.RbObligatorio.setSelected(this.unEquipamiento.getObligatorio());
        
        this.BtnBorrar.setEnabled(true);
        this.BtnCancelar.setEnabled(true);
        this.BtnGuardar.setEnabled(true);
        this.BtnNuevo.setEnabled(false);
        
                
        
    }//GEN-LAST:event_LstEquipamientoMouseClicked

    private void BtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelarActionPerformed
        // TODO add your handling code here:
        this.unEquipamiento = null;
        this.unInsumo = null;
        this.txtEquipamiento.setText("");
        this.BtnBorrar.setEnabled(false);
        this.BtnCancelar.setEnabled(false);
        this.BtnGuardar.setEnabled(false);
        this.BtnNuevo.setEnabled(true);
        this.cargarEquipamiento();
    }//GEN-LAST:event_BtnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmEquipamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmEquipamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmEquipamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmEquipamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmEquipamiento dialog = new FrmEquipamiento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnBorrar;
    private javax.swing.JButton BtnCancelar;
    private javax.swing.JButton BtnFindEquipamiento;
    private javax.swing.JButton BtnFindPersonal;
    private javax.swing.JButton BtnGuardar;
    private javax.swing.JButton BtnNuevo;
    private datechooser.beans.DateChooserCombo CmbFecha;
    private javax.swing.JList LstEquipamiento;
    private javax.swing.JRadioButton RbObligatorio;
    private javax.swing.JTextField TxtPersonal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txtEquipamiento;
    // End of variables declaration//GEN-END:variables
}
